// pages/techgoodlist/techgoodlist.js
var api = require('../../config/api.js');
var comm = require("../../utils/comm.js")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",

    startNum: 1, //页数
    total: '0',
    search: false, //判断模糊查询方法

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },

    curisOnSale: '',


  },
  // 搜索
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false,
      search: false
    });
    this.onLoad();

  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  searInput: function (e) {
    var _that = this
    console.log(_that.data.inputVal)
    wx.showLoading({
      title: '搜索中',
    })
    if (_that.data.inputVal.length != '') {
      wx.request({
        url: api.GoodsPagList + '?current=1&size=5',
        data: {
          goodsName: _that.data.inputVal,
        },
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'ACCESS-TOKEN': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          if (res.data.code == '1') {
            var listSize = res.data.obj.records.length;
            console.log('listSize=' + listSize);
            var newData = _that.data.myAgentList.list;
            var lis = res.data.obj.records;
            lis.forEach(function (item, index, array) {
              array[index] = {
                id: item.id,
                primaryPicUrl: item.primaryPicUrl,
                goodsName: item.goodsName,
                retailPrice: item.retailPrice,
                categoryName: item.categoryName,
                sellVolume: item.sellVolume,
                goodsBrief: item.goodsBrief
              }
            });
            newData = lis;
            var total1 = res.data.obj.total;
            var newList = {};
            //当长度小于10表示没有下页数据了
            if (listSize < 5) {
              newList.hasMore = false;
            } else {
              newList.hasMore = true;
            }
            newList.list = newData;
            console.log('newData=' + newData);
            newList.listSize = listSize;
            _that.setData({
              myAgentList: newList
            });
            console.log('myAgentList=' + _that.data.myAgentList);
            _that.setData({
              startNum: 2,
              total: total1,
              search: true
            });

            console.log("newList.list=" + newList.list)

            if (newList.list.length == 0) {
              wx.showModal({
                title: '提示',
                content: "抱歉！你搜索的商品不存在！",
                confirmColor: "#43CBC9",
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    _that.hideInput()
                  }
                }
              });
            }

          } else {
            // util.msg("错误", res.data.msg)
            console.log("网络通，数据不成功")
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res.data)
          // util.msg("错误", "通讯失败")
        }
      })

    } else {
      wx.showToast({
        title: "输入内容不能为空",
        icon: 'none',
        duration: 2000
      });
    }

  },

  maginfo: function (e) {
    wx.navigateTo({
      url: '/pages/techgoodlist/techgoodinfo/techgoodinfo?id=' + e.currentTarget.id,
    })
  },

  hotActivityLoadMore: function (e) {
    var _that = this;
    //判断是否还有下一页
    if (_that.data.myAgentList.hasMore == true) {
      wx.showLoading({
        title: '加载中',
      })
      var pageNum = _that.data.startNum
      var _url = 'current=' + pageNum + '&size=5';

      comm.request(api.GoodsPagList + _url, { userType: "4" }, "POST").then(function (res) {
        if (res.code == "1") {
          console.log("success")
          console.log("网络通，数据成功")
          wx.hideLoading();
          var listSize = res.obj.records.length;
          var newData = _that.data.myAgentList.list;
          var lis = res.obj.records;
          lis.forEach(function (item, index, array) {
            array[index] = {
              id:item.id,
              primaryPicUrl: item.primaryPicUrl,
              goodsName: item.goodsName,
              retailPrice: item.retailPrice,
              categoryName: item.categoryName,
              sellVolume: item.sellVolume,
              goodsBrief: item.goodsBrief
            }
          });
          //concat是在原有基础上进行增加
          newData = newData.concat(lis);
          var newList = {};
          //当长度等于于0表示当前没有数据，停止
          if (listSize == 0) {
            newList.hasMore = false;
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            return;
            //当长度小于10表示没有下页数据了
          } else if (listSize < 5) {
            wx.showToast({
              title: "已加载全部列表",
              icon: 'none',
              duration: 2000
            });
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList
          });

          _that.setData({
            startNum: pageNum + 1 //页数加1
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }

      }).catch(function(res){
        
        console.log("请求失败！")
      })
    } else {
      wx.showToast({
        title: "已加载全部商品",
        icon: 'none',
        duration: 2000
      });
    }
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var _that = this
    var _url = api.GoodsPagList + '?current=1&size=5';
    wx.showLoading({
      title: '加载中',
    })


    wx.request({
      url: _url,
      data: {
        current: "1", //请求第一页
        size: "5"
      },
      method: 'POST',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var listSize = res.data.obj.records.length;
          console.log('listSize=' + listSize);
          var newData = _that.data.myAgentList.list;
          var lis = res.data.obj.records;
          lis.forEach(function (item, index, array) {
            array[index] = {
              id: item.id,
              primaryPicUrl: item.primaryPicUrl,
              goodsName: item.goodsName,
              retailPrice: item.retailPrice,
              categoryName: item.categoryName,
              sellVolume: item.sellVolume,
              goodsBrief: item.goodsBrief
            }
          });
          newData = lis;
          var total1 = res.data.obj.total;
          var newList = {};
          //当长度小于5表示没有下页数据了
          if (listSize < 5) {
            newList.hasMore = false;
          } else {
            newList.hasMore = true;
          }
          newList.list = newData;
          console.log('newData=' + newData);
          newList.listSize = listSize;
          _that.setData({
            myAgentList: newList,
            total: total1
          });

          console.log('myAgentList=' + _that.data.myAgentList);

          _that.setData({
            startNum: 2
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})